package me.sheimi.sgit.repo.tasks.repo;


import android.util.Log;

import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.DepthWalk;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.FileTreeIterator;

import java.io.ByteArrayOutputStream;
import java.util.List;

import me.sheimi.sgit.R;
import me.sheimi.sgit.database.models.Repo;
import me.sheimi.sgit.exception.StopTaskException;

public class DiffLocalTask extends RepoOpTask {

    public String mFilePattern;

    public DiffLocalTask(Repo repo) {
        super(repo);
        setSuccessMsg(R.string.success_diff);
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        return diffLocal();
    }

    protected void onPostExecute(Boolean isSuccess) {
        super.onPostExecute(isSuccess);
    }

    public boolean diffLocal() {
        try {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

////            List<DiffEntry> diffEntries = mRepo.getGit().diff().setOutputStream(byteStream).call();
//            List<DiffEntry> diffEntries = mRepo.getGit().diff().call();
//            Log.i("keanbin", "Start");
//            for (DiffEntry entry : diffEntries) {
////                System.out.println(entry.getOldPath() + entry.getNewPath());
//                // 你可以进一步处理每个DiffEntry
//
//                DiffFormatter formatter = new DiffFormatter(byteStream) ;
//                formatter.setRepository(mRepo.getGit().getRepository());
//                formatter.setContext(0);
//                formatter.format(entry);
//            }
//
////            Log.i("keanbin", "toString = " + byteStream.toString());
//            Log.i("keanbin", "End");


            // 验证可以
            DiffFormatter formatter = new DiffFormatter( byteStream );
            formatter.setRepository( mRepo.getGit().getRepository() );
            AbstractTreeIterator commitTreeIterator = prepareTreeParser( mRepo.getGit().getRepository(),  Constants.HEAD );
            FileTreeIterator workTreeIterator = new FileTreeIterator( mRepo.getGit().getRepository() );
            List<DiffEntry> diffEntries = formatter.scan( commitTreeIterator, workTreeIterator );

            for( DiffEntry entry : diffEntries ) {
                Log.i("keanbin", "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId() );
                formatter.format( entry );
            }

            Log.i("keanbin", "toString = " + byteStream.toString());

        } catch (StopTaskException e) {
            return false;
        } catch (Throwable e) {
            setException(e);
            return false;
        }
        return true;
    }

    private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws Exception {
        Ref head = repository.getRef(ref);
        DepthWalk.RevWalk walk = new DepthWalk.RevWalk(repository, 0);
        RevCommit commit = walk.parseCommit(head.getObjectId());
        RevTree tree = walk.parseTree(commit.getTree().getId());

        CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
        ObjectReader oldReader = repository.newObjectReader();
        try {
            oldTreeParser.reset(oldReader, tree.getId());
        } finally {
            oldReader.release();
        }
        return oldTreeParser;
    }
}
